/*	FileChooserTable.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of MSGSwingSet ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.gui.component;

// Imports
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.table.DefaultTableModel;

import edu.bu.manateem.msg.MSGConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		May 13, 2010	- Version 1.0.
 */
public class FileChooserTableModel
	extends DefaultTableModel
{
	/*
	 * TYPES
	 */

	private enum FileChooserTableModelState
	{
		EMPTY,
		FULL
	}
	
	/*
	 * CONSTANTS
	 */

	// Serialize
	private static final long serialVersionUID = 1L;
	// Logging
	private static final Logger LOG =
		LoggerFactory.getLogger(FileChooserTableModel.class);
	
	// default values
	private static final String DEFAULT_FILE_PATH = MSGConstants.FILE_SEPARATOR;
	private static final String DEFAULT_FILE_NAME = "default";
	private static final String DEFAULT_FILE_TYPE = "txt";
	private static final String[] TABLE_COLUMNS =
		{
			"Path","File","Extension"
		};
	
	/*
	 * FIELDS
	 */

	private Set<String> files = new HashSet<String>();
	private FileChooserTableModelState state = FileChooserTableModelState.EMPTY;
	
	/*
	 * CONSTRUCTORS
	 */

	public FileChooserTableModel()
	{
		super(FileChooserTableModel.TABLE_COLUMNS,1);
		
		FileChooserTableModel.LOG.debug("init class");
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	public boolean addFile(File file)
		throws IOException
	{
		FileChooserTableModel.LOG.debug("adding file '{}' to table model",
			file.getName());
		try
		{
			String canon = file.getCanonicalPath();
			if (!files.contains(canon))
			{
				files.add(canon);
				String[] data =
						{
							getFilePath(canon),
							getFileName(canon),
							getFileType(canon)
						};
				insertRow(getRowCount(),data);
			}
			
			switch (state)
			{
				case EMPTY:
					// just added first file(s)
					state = FileChooserTableModelState.FULL;
					removeRow(0);
					break;
				case FULL:
					// add more files after first
				default:
					break;
			}
		}
		catch (IOException x)
		{
			throw new IOException("cannot buld canonical path for file"
				+ file.getName());
		}
		return false;
	}
	
	public void addFiles(File...files)
		throws IOException
	{
		FileChooserTableModel.LOG.debug("adding files to table model");
		for (File file : files)
		{
			addFile(file);
		}
	}
	
	public String getCanon(int row)
	{
		FileChooserTableModel.LOG.debug(
			"getting canonical path for file at {}",row);
		String path = (String) getValueAt(row,0);
		String name = (String) getValueAt(row,1);
		String type = (String) getValueAt(row,2);
		return path + name + "." + type;
	}
	
	public Set<String> getCanonicalPaths()
	{
		FileChooserTableModel.LOG.debug("getting canonical paths for files");
		return files;
	}
	
	private String getFileName(String canon)
	{
		FileChooserTableModel.LOG.debug("getting file name from {}",canon);
		String name = FileChooserTableModel.DEFAULT_FILE_NAME;
		int pathIndex = canon.lastIndexOf(MSGConstants.FILE_SEPARATOR);
		int typeIndex = canon.lastIndexOf('.');
		if ((pathIndex > 0) && (pathIndex < canon.length() - 1)
			&& (typeIndex > 0) && (typeIndex < canon.length() - 1))
		{
			name = canon.substring(pathIndex + 1,typeIndex);
		}
		
		return name;
	}
	
	private String getFilePath(String canon)
	{
		FileChooserTableModel.LOG.debug("getting file path from {}",canon);
		String path = FileChooserTableModel.DEFAULT_FILE_PATH;
		int pathIndex = canon.lastIndexOf(MSGConstants.FILE_SEPARATOR);
		if ((pathIndex > 0) && (pathIndex < canon.length() - 1))
		{
			path = canon.substring(0,pathIndex + 1);
		}
		
		return path;
	}
	
	private String getFileType(String canon)
	{
		FileChooserTableModel.LOG.debug("getting file extension from {}",canon);
		String type = FileChooserTableModel.DEFAULT_FILE_TYPE;
		int typeIndex = canon.lastIndexOf('.');
		if ((typeIndex > 0) && (typeIndex < canon.length() - 1))
		{
			type = canon.substring(typeIndex + 1);
		}
		
		return type;
	}
	
	@Override
	public boolean isCellEditable(int rowIndex,int colIndex)
	{
		FileChooserTableModel.LOG.debug("asking is cell[{},{}] editable",
			rowIndex,colIndex);
		return false;
	}
	
	public boolean removeRows(int...rows)
	{
		FileChooserTableModel.LOG.debug("removing rows");
		int removeCount = 0;
		for (int row : rows)
		{
			if (files.remove(getCanon(row - removeCount)))
			{
				FileChooserTableModel.LOG.debug("removing row {}",
					(row - removeCount));
				removeRow(row - removeCount);
				removeCount++;
			}
		}
		
		if (getRowCount() == 0)
		{
			state = FileChooserTableModelState.EMPTY;
			insertRow(0,new String[]
				{
					null,null,null
				});
		}
		
		return true;
	}
}
